package io.renren.sf.controller;


import io.renren.common.utils.ResponseData;
import io.renren.sf.entity.HzCityEntity;
import io.renren.sf.entity.HzFileEntity;
import io.renren.sf.service.HzCityService;
import io.renren.sf.service.HzFileService;
import io.renren.sf.service.HzTeacherService;
import io.renren.sf.web.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("resourcemanage")
@Api(tags="私人资源管理")
@ApiIgnore
public class PrivateController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    HzFileService hzFileService;

    @Autowired
    HzCityService hzCityService;

    @Autowired
    HzTeacherService hzTeacherService;

    @ApiOperation("创建文件")
    @PostMapping("/createfile")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileAddress", value = "文件地址src", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "fileSize", value = "文件大小", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "upperFid", value = "上层文件目录id,0为根目录", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名称", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileType", value = "文件类型0：目录1：文件", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData createfile(@RequestParam("fileAddress") String fileAddress,
                                   @RequestParam("fileSize") String fileSize,
                                   @RequestParam("upperFid") long upperFid,
                                   @RequestParam("fileName") String fileName,
                                   @RequestParam("fileType") int fileType,
                                   @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        boolean flag = false;
        if(upperFid == 0)
            //创建目录
            flag = hzFileService.createdir(upperFid, fileName, userId, fileType);
        else if(upperFid == 1)
            //创建文件
            flag = hzFileService.createfile(fileAddress, upperFid, fileName, userId, fileType, fileSize);
        return ResponseData.success(flag?"创建成功":"创建失败，文件名称不能重复！", flag);
    }

    @ApiOperation("查找当前目录下的文件")
    @GetMapping("/queryfilesbyfid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "upperFid", value = "上层文件目录id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<List<HzFileEntity>> queryfilesbyfid(@RequestParam("upperFid") long upperFid,
                                                            @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<HzFileEntity> hzFiles = hzFileService.queryfilesbyfid(upperFid, userId);
        if (hzFiles == null)    return ResponseData.success("该文件不存在或不是一个目录",false);
        return ResponseData.success(hzFiles);
    }

    @ApiOperation("修改文件名称")
    @GetMapping("/updatefilenamebyfid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fid", value = "文件id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "fileName", value = "修改的文件名称", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData updatefilenamebyfid(@RequestParam("fid") long fid,
                                            @RequestParam("fileName") String fileName,
                                            @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        HzFileEntity hzFileEntity = new HzFileEntity();
        hzFileEntity.setId(fid);
        hzFileEntity.setFileName(fileName);
        boolean flag = hzFileService.updateById(hzFileEntity);
        return ResponseData.success(flag?"更新成功":"更新失败",flag);
    }

    @ApiOperation("删除单个文件")
    @GetMapping("/deletebyfid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fid", value = "文件id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData deletebyfid(@RequestParam("fid") long fid,
                                    @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        boolean flag = hzFileService.deletebyfid(fid, userId);
        return ResponseData.success(flag?"删除成功":"删除失败",flag);
    }

    @ApiOperation("删除多个文件")
    @PostMapping("/deletefilesbyids")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "文件ids", paramType = "query", dataType = "array[Long]", allowMultiple = true, required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData deletefilesbyids(@RequestParam("ids") List<Long> ids,
                                    @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        boolean flag = hzFileService.deletefilesbyids(ids, userId);
        return ResponseData.success(flag?"删除成功":"删除失败",flag);
    }

    @ApiOperation("文件增加下载量")
    @GetMapping("/addfiledowncount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fid", value = "文件id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData addfiledowncount(@RequestParam("fid") long fid,
                                    @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        boolean flag = hzFileService.addfiledowncount(fid);
        return ResponseData.success(flag?"下载量增加成功":"下载量增加失败",flag);
    }

    @ApiOperation("获取省、市、区/县json数据")
    @GetMapping("/getPCA")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<List<HzFileEntity>> getPCA(@RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<HzCityEntity> citys = hzCityService.getPCA();
        return ResponseData.success(citys);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<UserInfo> getUserInfo(@RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        UserInfo userInfo = hzTeacherService.getUserInfo(userId);
        return ResponseData.success(userInfo);
    }

    @ApiOperation("提交资源到公共资源上")
    @PostMapping("/uploadfiletopublic")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileAddress", value = "文件地址src", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileSize", value = "文件大小", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名称", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "phone", value = "联系方式", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "teacherName", value = "教师名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "unitName", value = "单位名", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pid", value = "省地址id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "cid", value = "市地址id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "aid", value = "区/县地址id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData uploadfiletopublic(@RequestParam("fileAddress") String fileAddress,
                                           @RequestParam("fileSize") String fileSize,
                                           @RequestParam("fileName") String fileName,
                                           @RequestParam("phone") String phone,
                                           @RequestParam("teacherName") String teacherName,
                                           @RequestParam("unitName") String unitName,
                                           @RequestParam("pid") long pid,
                                           @RequestParam("cid") long cid,
                                           @RequestParam("aid") long aid,
                                           @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        HzFileEntity hzFileEntity = new HzFileEntity(fileAddress, fileName, phone, teacherName, unitName, pid, cid, aid, userId, fileSize);
        boolean flag = hzFileService.save(hzFileEntity);
        return ResponseData.success(flag?"创建成功":"创建失败", flag);
    }


    @ApiOperation("按照名称查找文件")
    @GetMapping("/queryfilesbyname")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "filename", value = "关键词", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "fileBelongto", value = "文件所属0：公共1：私人", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<List<HzFileEntity>> queryfilesbyname(@RequestParam("filename") String filename,
                                                             @RequestParam("fileBelongto") int fileBelongto,
                                                            @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<HzFileEntity> hzFiles = hzFileService.queryfilesbyname(filename, userId, fileBelongto);
        return ResponseData.success(hzFiles);
    }

    @ApiOperation("根据状态获取上传的公共教参")
    @GetMapping("/queryfilesbystate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "verifyState", value = "审核状态0：未审核1：已审核2：审核失败3：全部", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData<List<HzFileEntity>> queryfilesbystate(@RequestParam("verifyState") int verifyState,
                                                            @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));
        List<HzFileEntity> hzFiles = hzFileService.queryfilesbystate(verifyState, userId);
        return ResponseData.success(hzFiles);
    }

    @ApiOperation("移动文件到指定文件夹下")
    @GetMapping("/changefiletodir")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fid", value = "文件id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "did", value = "文件夹id", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "Authorization",value = "token",paramType = "header",dataType = "String", required = true)
    })
    public ResponseData changefiletodir(@RequestParam("fid") long fid,
                                         @RequestParam("did") long did,
                                         @RequestHeader("Authorization") String token){
        long userId = Long.parseLong(redisTemplate.opsForValue().get("user:" + token));

        boolean flag = hzFileService.changefiletodir(fid,did,userId);

        return ResponseData.success(flag?"移动成功":"移动失败", flag);
    }
}
